﻿using Messenger.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Messenger.Views
{
    /// <summary>
    /// UserControlBottom.xaml 的交互逻辑
    /// </summary>
    public partial class UserControlBottom : UserControl
    {
        private CancellationTokenSource cts = new CancellationTokenSource();
        UserControlBottomViewModel viewModel = new UserControlBottomViewModel();
        public UserControlBottom()
        {
            InitializeComponent();
        }

        private async void Display_Click(object sender, RoutedEventArgs e)
        {
            cts = new CancellationTokenSource();
            var token = cts.Token;

            try
            {
                await Task.Run(async () => 
                {
                    for (int i = 0; i < 10; i++) 
                    {
                        if (token.IsCancellationRequested)
                        {
                            Dispatcher.Invoke(() => 
                            {
                                tb1.Text = "操作取消";
                            });
                            return;
                        }
                        await Task.Delay(500);
                        Dispatcher.Invoke(() => 
                        {
                            tb1.Text = $"Processing...{i + 1}/10";
                        });
                    }
                    Dispatcher.Invoke(() => 
                    {
                        tb1.Text = "Hello World";
                    });
                },token);
            }
            catch (OperationCanceledException) 
            {
                Dispatcher.Invoke(() => { tb1.Text = "发生错误，操作取消"; });
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            if(cts != null)
            {
                cts.Cancel();
            }
        }
        private bool isOver = false;

        private void ImageConvert_Click(object sender, RoutedEventArgs e)
        {
            isOver = true;
        }
    }
}
